package com.woniuxy.oa.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.woniuxy.oa.entity.Employee;
import com.woniuxy.oa.entity.PageBeanList;
import com.woniuxy.oa.entity.WorkPlan;
import com.woniuxy.oa.exception.WorkPlanException;
import com.woniuxy.oa.service.WorkPlanService;

@Controller
public class WorkPlanController {
	
	@Autowired
	WorkPlanService workPlanService;
	
	/**
	 * 显示
	 * @param model
	 * @return
	 */
	@GetMapping("/workPlan/getAllWorkPlan")
	public String getAllWorkPlan(Model model) {
		List<WorkPlan> workPlans=null;
		try {
			workPlans = workPlanService.getAllWorkPlan();
			model.addAttribute("workPlans", workPlans);
		} catch (WorkPlanException e) {
			model.addAttribute("msg", "系统错误!请联系管理员");
			return "msg";
		}
		return "system/work.html";
	}
	
	/**
	 * 跳转添加WorkPlan页面
	 * @return
	 */
	@GetMapping("/workPlan/addWorkPlan")
	public String skipAddWorkPlan() {
		return "addWorkPlan";
	}
	
	/**
	 * 这是添加workPlan方法
	 * @return
	 */
	@PostMapping("/workPlan/addWorkPlan")
	public String addWorkPlan(HttpSession session,String w_summary,
			String w_problem,String w_plan) {
		//后面整合了  username取当前登录用户    session里面取值
		WorkPlan workPlan=new WorkPlan();
		workPlan.setW_summary(w_summary);
		workPlan.setW_problem(w_problem);
		workPlan.setW_plan(w_plan);
		Employee employee=(Employee) session.getAttribute("user");
		workPlan.setEmployee(employee);
		workPlanService.addWorkPlan(workPlan);
		return "redirect:/workPlan/getAllPageBean";
	}
	
	/**
	 * 修改工作计划
	 * @param w_id
	 * @return
	 */
	@GetMapping("/workPlan/{w_id}")
	public String updateWorkPlan(@PathVariable("w_id")Integer w_id,
			Model model) {
		WorkPlan workPlan = workPlanService.getWorkPlanByW_id(w_id);
		model.addAttribute("workPlan", workPlan);
		return "updateWorkPlan";
	}
	
	/**
	 * 修改工作计划
	 * @param w_id
	 * @return
	 */
	//@PutMapping("/workPlan")
	@RequestMapping("/workPlan")
	public String modifyWorkPlan(WorkPlan workPlan) {
		workPlanService.modifyWoekPlan(workPlan);
		return "redirect:/workPlan/getAllPageBean";
	}
	
	/**
	 * 删除
	 * @param w_id
	 * @return
	 */
	@DeleteMapping("/workPlan/{w_id}")
	public String removeWorkPlan(@PathVariable("w_id")Integer w_id) {
		workPlanService.removeWorkPlan(w_id);
		return "redirect:/workPlan/getAllPageBean";
	}
	
	/**
	 * 分页显示
	 * @param model
	 * @return
	 */
	@GetMapping("/workPlan/getAllPageBean")
	public String getAllPageBean(Model model,HttpServletRequest request,
			WorkPlan workPlan,Employee employee) {
		try {
			//把查询条件加进workplan里面
			workPlan.setEmployee(employee);
			//写的方法中获取当前页码
			int pageIndex=getPageIndex(request);
			//定义每页显示多大
			int pageSize=10;
			//调用service方法获取模糊查询信息
			PageBeanList<WorkPlan> pb = workPlanService
					.getWorkPlanConditionByPage(pageIndex, pageSize, workPlan);
			//获取每个页码的请求路径
			pb.setUrl(getUrl(request));
			//把对象存进域中
			model.addAttribute("pb", pb);
			//回显
			model.addAttribute("workPlan", workPlan);
			return "system/work.html";
		} catch (WorkPlanException e) {
			model.addAttribute("msg", "系统错误!请联系管理员");
			return "msg";
		}
	}
	
	/**
	 * 获取当前页码的方法
	 * @param request
	 * @return
	 */
	private int getPageIndex(HttpServletRequest request) {
		//如果没有传页码  就按第一页显示
		if(request.getParameter("pageIndex")==null || request.getParameter("pageIndex").equals("")) {
			return 1;
		}
		//把页码转为integer类型返回回去
		return Integer.parseInt(request.getParameter("pageIndex"));
	}
	
	/**
	 * 获取每个页码的请求路径
	 * @param request
	 * @return
	 */
	private String getUrl(HttpServletRequest request) {
//		假定你的web application 名称为news,你在浏览器中输入请求路径：
//		http://localhost:8080/news/main/list.jsp
//		则执行下面向行代码后打印出如下结果：
//		1、 System.out.println(request.getContextPath());
//		打印结果：/news
//		   2、System.out.println(request.getServletPath());
//		打印结果：/main/list.jsp
//		比如客户端发送
//		http://localhost/test.do?a=b&c=d&e=f
//		通过request.getQueryString()得到的是
//		a=b&c=d&e=f
//		API 中注释写到：’
//		意思是：获取带参数查询。
		String contextPath = request.getContextPath();
		String servletPath = request.getServletPath();
		String queryString = request.getQueryString();
		//判断传参不为空   就是判断有没有传参
		if( queryString!=null && queryString.indexOf("&pageIndex=")!=-1) {
			//这个就是截取这个&pageIndex参数之前的参数
			queryString=queryString.substring(0, queryString.indexOf("&pageIndex="));
		}
		return contextPath+servletPath+"?"+queryString;
	}
}
